package top.zackyoung.mybatisplus.query.controller;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.zackyoung.mybatisplus.query.entity.Role;
import top.zackyoung.mybatisplus.query.entity.User;
import top.zackyoung.mybatisplus.query.entity.UserRole;
import top.zackyoung.mybatisplus.query.mapper.UserMapper;
import top.zackyoung.mybatisplus.query.mapper.UserRoleMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @description
 * @author: ZackYoung
 * @date 2021/5/3 18:07
 */
@RestController
@RequestMapping("/query")
public class TestController {
    @Resource
    UserRoleMapper userRoleMapper;
    @Resource
    UserMapper userMapper;

    @RequestMapping("/simpleQuery/{userId}")
    public List<Role> simpleQuery(@PathVariable Long userId) {
        return userRoleMapper.selectJoinList(Role.class,
                new MPJLambdaWrapper<UserRole>().leftJoin(User.class, User::getId, UserRole::getUserId)
                        .selectAll(Role.class)
                        .leftJoin(Role.class, Role::getId, UserRole::getRoleId)
                        .eq(UserRole::getUserId, userId)
        );
    }

    @RequestMapping("/simpleCusQuery/{userId}")
    public User simpleCusQuery(@PathVariable Long userId) {
        return userMapper.selectOne(userMapper.lQ().eq(User::getId, userId));
    }

}
